package com.huifaapp.mall.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.AlreadyPayAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.AlreadyPayBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

public class AlreadyPayActivity extends AppCompatActivity {

    //  private RecyclerView Realready;
    //private List<PendingDeliveryBean.DataBean> goods;
    private TextView recename,recephone,receaddress,runprice,redbag,sjprice;
    private TextView creatpay,ddnum,zfjynum;
    private AlreadyPayBean.DataBean goods;
    private List<AlreadyPayBean.DataBean.OrderItemBean> orderdata;
    private LinearLayout linearLayout;
    String orderNo;
    private Button bt_kefu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_already_pay);
        orderNo=getIntent().getStringExtra("orderNo");
        Log.e("ppppppp",orderNo);
        initView();

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("待发货详情",ApiManager.BASE_NIUPAI_URL+"orders/notsendeddetail?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(AlreadyPayActivity.this)+"&orderNo="+orderNo);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notsendeddetail")
//                ?token=201712291124171334f3ed61d93e05404b8dc78f10c4770cf8&appId=D7B72681-8205-4C8F-8CCB-142C448203E3&orderNo="+orderNo)." +
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(AlreadyPayActivity.this))
                .addParams("orderNo",orderNo)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    Gson gson = new Gson();
                    AlreadyPayBean alreadyPayBean = gson.fromJson(response,AlreadyPayBean.class);
                    goods = alreadyPayBean.getData();
                    orderdata = goods.getOrderItem();
                    createShopItem(linearLayout,orderdata);
                    //  AlreadyPayAdapter alreadyPayAdapter = new AlreadyPayAdapter(AlreadyPayActivity.this,goods,orderdata);
//                    Realready.setLayoutManager(new LinearLayoutManager(AlreadyPayActivity.this));
//                    Realready.setAdapter(alreadyPayAdapter);
                    recename.setText(alreadyPayBean.getData().getReceiverName());
                    recephone.setText(alreadyPayBean.getData().getReceiverPhone()+"");
                    receaddress.setText(alreadyPayBean.getData().getReceiverAddress());
                    runprice.setText(alreadyPayBean.getData().getExpressfee()+"");
                    redbag.setText(alreadyPayBean.getData().getCouponAmount()+"");
                    sjprice.setText(alreadyPayBean.getData().getActualpay()+"");
                    zfjynum.setText(alreadyPayBean.getData().getPayno()+"");
                    ddnum.setText(alreadyPayBean.getData().getOrderNo()+"");
                    //   startpay.setText(alreadyPayBean.getData().getPaytime()+"");
                    creatpay.setText(alreadyPayBean.getData().getCreatetime()+"");
                }catch (Exception e){
                    e.printStackTrace();
                    ToastUtils.showShort("获取失败");
                }



            }

        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("待发货详情",ApiManager.BASE_NIUPAI_URL+"orders/notsendeddetail?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(AlreadyPayActivity.this)+"&orderNo="+orderNo);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notsendeddetail")
//                ?token=201712291124171334f3ed61d93e05404b8dc78f10c4770cf8&appId=D7B72681-8205-4C8F-8CCB-142C448203E3&orderNo="+orderNo)." +
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(AlreadyPayActivity.this))
                .addParams("orderNo",orderNo)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    Gson gson = new Gson();
                    AlreadyPayBean alreadyPayBean = gson.fromJson(response,AlreadyPayBean.class);
                    goods = alreadyPayBean.getData();
                    orderdata = goods.getOrderItem();
                    createShopItem(linearLayout,orderdata);
                    //  AlreadyPayAdapter alreadyPayAdapter = new AlreadyPayAdapter(AlreadyPayActivity.this,goods,orderdata);
//                    Realready.setLayoutManager(new LinearLayoutManager(AlreadyPayActivity.this));
//                    Realready.setAdapter(alreadyPayAdapter);
                    recename.setText(alreadyPayBean.getData().getReceiverName());
                    recephone.setText(alreadyPayBean.getData().getReceiverPhone()+"");
                    receaddress.setText(alreadyPayBean.getData().getReceiverAddress());
                    runprice.setText(alreadyPayBean.getData().getExpressfee()+"");
                    redbag.setText(alreadyPayBean.getData().getCouponAmount()+"");
                    sjprice.setText(alreadyPayBean.getData().getActualpay()+"");
                    zfjynum.setText(alreadyPayBean.getData().getPayno()+"");
                    ddnum.setText(alreadyPayBean.getData().getOrderNo()+"");
                    //   startpay.setText(alreadyPayBean.getData().getPaytime()+"");
                    creatpay.setText(alreadyPayBean.getData().getCreatetime()+"");
                }catch (Exception e){
                    e.printStackTrace();
                    ToastUtils.showShort("获取失败");
                }



            }

        });
    }

    private void initView() {
//        Realready = (RecyclerView) findViewById(R.id.Reready);
        linearLayout = (LinearLayout) findViewById(R.id.already_pay_liner);
        recename = (TextView) findViewById(R.id.already_recename);
        recephone = (TextView) findViewById(R.id.already_phone);
        receaddress = (TextView) findViewById(R.id.already_address);
        runprice = (TextView) findViewById(R.id.already_runprice);
        redbag = (TextView) findViewById(R.id.already_bagprice);
        sjprice = (TextView) findViewById(R.id.already_sjprice);
        //startpay = (TextView) findViewById(R.id.already_startpaytime);
        creatpay = (TextView) findViewById(R.id.already_creatpaytime);
        ddnum = (TextView) findViewById(R.id.already_ddnum);
        zfjynum = (TextView) findViewById(R.id.already_zfjjnum);
        bt_kefu = (Button) findViewById(R.id.already_lianxikefu);



        bt_kefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AlreadyPayActivity.this, com.huifaapp.mall.activity.Homefragment.WebActivity.class);
                intent.putExtra("url",ApiManager.KEFU);
                startActivity(intent);
            }
        });
//        Intent intent = getIntent();
//        String orderNo = intent.getStringExtra("orderNo");
        Log.e("待发货详情",ApiManager.BASE_NIUPAI_URL+"orders/notsendeddetail?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(AlreadyPayActivity.this)+"&orderNo="+orderNo);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notsendeddetail")
//                ?token=201712291124171334f3ed61d93e05404b8dc78f10c4770cf8&appId=D7B72681-8205-4C8F-8CCB-142C448203E3&orderNo="+orderNo)." +
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(AlreadyPayActivity.this))
                .addParams("orderNo",orderNo)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    Gson gson = new Gson();
                    AlreadyPayBean alreadyPayBean = gson.fromJson(response,AlreadyPayBean.class);
                    goods = alreadyPayBean.getData();
                    orderdata = goods.getOrderItem();
                    createShopItem(linearLayout,orderdata);
                    //  AlreadyPayAdapter alreadyPayAdapter = new AlreadyPayAdapter(AlreadyPayActivity.this,goods,orderdata);
//                    Realready.setLayoutManager(new LinearLayoutManager(AlreadyPayActivity.this));
//                    Realready.setAdapter(alreadyPayAdapter);
                    recename.setText(alreadyPayBean.getData().getReceiverName());
                    recephone.setText(alreadyPayBean.getData().getReceiverPhone()+"");
                    receaddress.setText(alreadyPayBean.getData().getReceiverAddress());
                    runprice.setText(alreadyPayBean.getData().getExpressfee()+"");
                    redbag.setText(alreadyPayBean.getData().getCouponAmount()+"");
                    sjprice.setText(alreadyPayBean.getData().getActualpay()+"");
                    zfjynum.setText(alreadyPayBean.getData().getPayno()+"");
                    ddnum.setText(alreadyPayBean.getData().getOrderNo()+"");
                 //   startpay.setText(alreadyPayBean.getData().getPaytime()+"");
                    creatpay.setText(alreadyPayBean.getData().getCreatetime()+"");
                }catch (Exception e){
                    e.printStackTrace();
                    ToastUtils.showShort("获取失败");
                }



            }

        });
    }

    private void createShopItem(ViewGroup viewGroup, List<AlreadyPayBean.DataBean.OrderItemBean> lists){
        if (lists == null) {
            return;
        }
        viewGroup.removeAllViews();
        for (int i = 0; i < lists.size(); i++) {

            final AlreadyPayBean.DataBean.OrderItemBean orderItemBean = orderdata.get(i);
            Log.e("售后orderno",goods.getOrderNo());
            Log.e("售后sku",orderItemBean.getSkuId());

            View view =  LayoutInflater.from(this).inflate(R.layout.alreadypay_item,null);
            final AlreadyPayAdapter.MyViewHolder viewHolder = new AlreadyPayAdapter.MyViewHolder(view);
            final String dealstate = orderItemBean.getDealState();

            Log.e("shouhuoxiangqing",dealstate);

            if (dealstate.equals("001")){
                viewHolder.bt_money.setText("退款中");
            }else if (dealstate.equals("002")){
                viewHolder.bt_money.setText("换货关闭/退货关闭");
            }else if (dealstate.equals("003")){
                viewHolder.bt_money.setText("待处理");
            }else if (dealstate.equals("004")){
                viewHolder.bt_money.setText("售后拒绝");
            }else if (dealstate.equals("005")){
                viewHolder.bt_money.setText("同意换货填写物流");
            }else if (dealstate.equals("006")){
                viewHolder.bt_money.setText("退款成功");
            }


            viewHolder.bt_money.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e("dealstate",dealstate );
                    if (dealstate.equals("0")||dealstate.equals("007")){
                        Intent intent = new Intent(AlreadyPayActivity.this,AfterSaleStateActivity.class);
                        intent.putExtra("orderNo",goods.getOrderNo());
                        intent.putExtra("state",goods.getState());
                        intent.putExtra("skuId",orderItemBean.getSkuId()+"");
                        intent.putExtra("goodsid",orderItemBean.getGoodsId()+"");
                        startActivity(intent);
                    }else {
//                        Intent intent = new Intent(AlreadyPayActivity.this,AfterStatusActivity.class);
//                        intent.putExtra("aftersaleId",orderItemBean.getAftersaleId()+"");
//                        Log.e("daishouhuoxq",orderItemBean.getAftersaleId()+"");
//                        startActivity(intent);
                    }



                }
            });

            viewHolder.setData(this, lists.get(i));
            viewGroup.addView(view);
        }
    }


}